package com.fdb.efp.nls.service.impl;

import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.basic.framework.core.util.TimeUtil;
import com.fdb.efp.nls.common.ConsoleConstant;
import com.fdb.efp.flow.common.vo.PageApproveVO;
import com.fdb.efp.flow.service.facade.BizEventInfoService;
import com.fdb.efp.limit.service.facade.LmtPrdContService;
import com.fdb.efp.limit.service.vo.LmtPrdContVO;
import com.fdb.efp.nls.service.dao.LmtUnfreezeAppDao;
import com.fdb.efp.nls.service.domain.LmtUnfreezeApp;
import com.fdb.efp.nls.service.facade.LmtUnfreezeAppApprService;
import com.fdb.efp.nls.service.vo.LmtUnfreezeAppVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service("lmtUnfreezeAppApprService")
public class LmtUnfreezeAppApprServiceImpl implements LmtUnfreezeAppApprService, FrameworkService {

    private static Logger logger = LoggerFactory.getLogger(LmtUnfreezeAppApprServiceImpl.class);

    @Autowired
    private LmtUnfreezeAppDao lmtUnfreezeAppDao;

//    @Autowired
//    private LmtPrdContDao lmtPrdContDao;

    @Autowired
    @Qualifier("lmtPrdContService")
    private LmtPrdContService lmtPrdContService;

    @Autowired
    @Qualifier("bizEventInfoService")
    private BizEventInfoService bizEventInfoService;

    @Override
    public void pass(String bizSerno, PageApproveVO pageApproveVo) {
        logger.info("额度解冻通过流水号" + bizSerno);
        try {
            if (Objects.nonNull(bizSerno) && !"".equals(bizSerno)) {
                LmtUnfreezeApp vo = new LmtUnfreezeApp();
                vo.setUnfrzSerno(bizSerno);
                LmtUnfreezeApp lmtUnfreezeApp = lmtUnfreezeAppDao.queryByPk(vo);
                LmtUnfreezeAppVO qvo = new LmtUnfreezeAppVO();
                beanCopy(lmtUnfreezeApp, qvo);
                if (Objects.nonNull(qvo)) {
                    qvo.setLastUpdateTime(TimeUtil.getCurrentDateTime());
                    qvo.setLastUpdateUser(pageApproveVo.getAprvUserId());
                    // 审批用户编号
                    qvo.setAprvUserCode(pageApproveVo.getAprvUserId());
                    qvo.setAprvUserName(pageApproveVo.getAprvUserName());
                    // 审批意见
                    qvo.setAprvComment(pageApproveVo.getAprvComment());
                    // 审批状态 = 通过
                    qvo.setApproveStatus(ConsoleConstant.APRV_STATUS_03);
                    // 审批时间
                    qvo.setAprvDate(TimeUtil.getCurrentDate());
                }
                beanCopy(qvo, lmtUnfreezeApp);
                lmtUnfreezeAppDao.updateByPk(lmtUnfreezeApp);

                // 更新授信协议信息表 修改 额度状态为‘有效‘
                LmtPrdContVO lmtPrdContVO = new LmtPrdContVO();
                String lmtContNo = lmtUnfreezeApp.getLmtContNo();
                lmtPrdContVO.setLmtContNo(lmtContNo);
                lmtPrdContVO = lmtPrdContService.queryByPk(lmtPrdContVO);
                if(Objects.isNull(lmtPrdContVO)){
                    logger.error("额度解冻审批通过流程后处理，通过授信协议号:"+ lmtContNo +"查询到的授信协议信息为空！");
                    return;
                }
                // 修改授信协议表  额度状态为“有效”
                lmtPrdContVO.setLmtStatus(ConsoleConstant.LIMIT_STS_02);
                int num = lmtPrdContService.updateByPk(lmtPrdContVO);
                if(num < 0){
                    logger.error("额度解冻审批通过后处理，通过授信协议号:"+ lmtContNo +"，更新授信协议表额度状态失败！");
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void refuse(String bizSerno, PageApproveVO pageApproveVo) {
        logger.info("额度解冻拒绝流水号" + bizSerno);
        try {
            if (Objects.nonNull(bizSerno) && !"".equals(bizSerno)) {
                LmtUnfreezeApp vo = new LmtUnfreezeApp();
                vo.setUnfrzSerno(bizSerno);
                LmtUnfreezeApp lmtUnfreezeApp = lmtUnfreezeAppDao.queryByPk(vo);
                LmtUnfreezeAppVO qvo = new LmtUnfreezeAppVO();
                beanCopy(lmtUnfreezeApp, qvo);
                if (Objects.nonNull(qvo)) {
                    qvo.setLastUpdateTime(TimeUtil.getCurrentDateTime());
                    qvo.setLastUpdateUser(pageApproveVo.getAprvUserId());
                    // 审批用户编号
                    qvo.setAprvUserCode(pageApproveVo.getAprvUserId());
                    qvo.setAprvUserName(pageApproveVo.getAprvUserName());
                    // 审批意见
                    qvo.setAprvComment(pageApproveVo.getAprvComment());
                    // 审批状态 = 拒绝
                    qvo.setApproveStatus(ConsoleConstant.APRV_STATUS_04);
                    // 审批时间
                    qvo.setAprvDate(TimeUtil.getCurrentDate());
                }
                beanCopy(qvo, lmtUnfreezeApp);
                lmtUnfreezeAppDao.updateByPk(lmtUnfreezeApp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public void repulse(String bizSerno, PageApproveVO pageApproveVo) {
        try {
            if (bizEventInfoService.isFirstNode(bizSerno)) {
                logger.info("额度解冻打回流水号" + bizSerno);
                if (Objects.nonNull(bizSerno) && !"".equals(bizSerno)) {
                    LmtUnfreezeApp vo = new LmtUnfreezeApp();
                    vo.setUnfrzSerno(bizSerno);
                    LmtUnfreezeApp lmtUnfreezeApp = lmtUnfreezeAppDao.queryByPk(vo);
                    LmtUnfreezeAppVO qvo = new LmtUnfreezeAppVO();
                    beanCopy(lmtUnfreezeApp, qvo);
                    if (Objects.nonNull(qvo)) {
                        qvo.setLastUpdateTime(TimeUtil.getCurrentDateTime());
                        qvo.setLastUpdateUser(pageApproveVo.getAprvUserId());
                        // 审批用户编号
                        qvo.setAprvUserCode(pageApproveVo.getAprvUserId());
                        qvo.setAprvUserName(pageApproveVo.getAprvUserName());
                        // 审批意见
                        qvo.setAprvComment(pageApproveVo.getAprvComment());
                        // 审批状态 = 打回
                        qvo.setApproveStatus(ConsoleConstant.APRV_STATUS_05);
                        // 审批时间
                        qvo.setAprvDate(TimeUtil.getCurrentDate());
                    }
                    beanCopy(qvo, lmtUnfreezeApp);
                    lmtUnfreezeAppDao.updateByPk(lmtUnfreezeApp);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


